<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class UploadController
{
    public function image(Request $request): array
    {
        $file = $request->file('file');
        if (!$file) abort(403, '文件不存在');

        $path = $file->store("images/" . date('Ymd'), ['disk' => 'public']);

        return [
            'data' => [
                'url' => Storage::disk('public')->url($path), // 自动使用配置的域名
                'error' => ['message' => null],
            ],
            'code' => 200,
        ];
    }

    public function avatar(Request $request): array
    {
        $file = $request->file('file');
        if (!$file) abort(403, '文件不存在');

        $path = $file->store("avatar", ['disk' => 'public']);
        return [
            'data' => [
                'url' => Storage::disk('public')->url($path), // 自动使用配置的域名
            ],
            'message' => 'success',
            'code' => 200,
        ];
    }
}
